package ch.supsi.template.money;

public class Money extends AbstractMoney {
    private int fAmount;
    private String fCurrency;

    public Money(int amount, String currency) {
        fAmount = amount;
        fCurrency = currency;
    }

    public int getAmount() {
        return fAmount;
    }

    public String getCurrency() {
        return fCurrency;
    }

    /* prima variante semplice

    public Money add(Money m) {
       return new Money(amount()+m.amount(), currency());
    } */

    /* seconda variante (con errore di return)

    public Money add(Money m) {
    if (m.currency().equals(currency()) )
        return new Money(amount()+m.amount(), currency());
    return new MoneyBag(this, m);
}
*/

    //da IMoney


    public IMoney addMoney(Money m) {
        if (m.getCurrency().equals(getCurrency())) {
            return new Money(getAmount() + m.getAmount(), getCurrency());
        }
        return new MoneyBag(this, m);
    }


    public IMoney addMoneyBag(MoneyBag b) {
        return b.addMoney(this);
    }

    //per Visitor

    /*
    public IMoney add(IMoney m) {
            return ((AbstractMoney)m).addMoney(this);
        }
        */

    public boolean equals(Object other) {
        return ((other instanceof Money) &&
                ((Money) other).getCurrency().equals(getCurrency()) &&
                ((Money) other).getAmount() == getAmount());
    }

    public IMoney multiply(int factor) {
        return new Money(getAmount() * factor, getCurrency());
    }

}
